package com.coastwe.dahua.chapter04;

public class ArrayStack {
    private int[] data;
    private int n;
    private int top;

    public ArrayStack(int capacity) {
        data = new int[capacity];
        this.n = capacity;
        this.top = 0;
    }

    public boolean push(int value) {
        if (top == n) {
            System.out.println("堆栈已经满了");
            return false;
        }
        data[top++] = value;
        return true;
    }

    public int pop() {
        if (top == 0) {
            System.out.println("堆栈已经空了");
        }
        return data[--this.top];
    }

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
    }
}